using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace BWClient
{
    public partial class ShowStatus : DevComponents.DotNetBar.Office2007Form
    {
        public ShowStatus()
        {
            InitializeComponent();
        }

        BWClientData oBWClientData = null;

        bool bIsLogined = false;

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            oBWClientData = BWClientDataManager.getCurrentBWClientData();

            if (oBWClientData.AutoLogin)
            {
                int iUserID = 0;
                CommonHelper.UserLoginResult oUserLoginResult = CommonHelper.UserLogin(oBWClientData.UserName, CommonHelper.DESDecode(oBWClientData.Password, BWClientDataManager.DesKey), false, ref iUserID);
                oBWClientData.iUserID = iUserID;

                if (oUserLoginResult == CommonHelper.UserLoginResult.LogonOK)
                {
                    bIsLogined = true;
                }
            }
        }

        private void ShowStatus_Load(object sender, EventArgs e)
        {
            Application.DoEvents();
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            HideThisDialog();
        }

        public delegate void FlushDialog();
        private void HideThisDialog()
        {
            if (this.InvokeRequired)
            {
                FlushDialog oFlushWarnning = new FlushDialog(HideThisDialog);
                this.Invoke(oFlushWarnning);
            }
            else
            {

                this.Hide();

                if (bIsLogined)
                    (new MainForm { m_BWClientData = oBWClientData }).ShowDialog();
                else
                {
                    (new UserLoginDialog()).ShowDialog();
                }
                
                Application.ExitThread();
            }
        }
    }
}